package com.ctgu.chatapp.view.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ctgu.chatapp.R
import com.ctgu.chatapp.Service.RequestService
import com.ctgu.chatapp.databinding.ItemReceiveBinding
import com.ctgu.chatapp.databinding.ItemSentBinding
import com.ctgu.chatapp.model.pojo.RecordNew

class RecordAdapter(
    var recordList: MutableList<RecordNew>,
    private val friendProfile: String,
    private val userProfile: String
) :
    RecyclerView.Adapter<RecordAdapter.ViewHolder>() {

    private lateinit var mRecycleView: RecyclerView

    class ViewHolder(val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = run {
        if (viewType == R.layout.item_sent) {
            ViewHolder(ItemSentBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        } else {
            ViewHolder(
                ItemReceiveBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (recordList[position].fromSelf) {
            true -> {
                (holder.binding as ItemSentBinding).record = recordList[position]
                holder.binding.setProfile(userProfile)
                holder.binding.layout.setOnClickListener { it ->
                    val popupMenu = PopupMenu(it.context, it)
                    popupMenu.menuInflater.inflate(R.menu.record, popupMenu.menu)
                    popupMenu.setOnMenuItemClickListener {
                        if (it.itemId == R.id.reback) {
                            RequestService.sendControlRequest(
                                recordList[position].selfID,
                                recordList[position].friendID,
                                recordList[position].snowFlower
                            )
                            recordList.removeAt(position)
                            notifyItemRemoved(position)
                            true
                        } else
                            false
                    }
                    popupMenu.show()
                    true
                }
            }
            else -> {
                (holder.binding as ItemReceiveBinding).record = recordList[position]
                holder.binding.setProfile(friendProfile)
            }
        }
    }

    override fun getItemViewType(position: Int) = run {
        when (recordList[position].fromSelf) {
            true -> R.layout.item_sent
            else -> R.layout.item_receive
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mRecycleView = recyclerView
    }

    override fun getItemCount() = recordList.size

    fun addAll(list: MutableList<RecordNew>) {
        recordList.addAll(list)
        notifyItemRangeInserted(itemCount, list.size)
    }

    fun addOldRecord(list: MutableList<RecordNew>) {
        recordList.addAll(0, list)
        notifyItemRangeInserted(0, list.size)
    }

    fun addMessage(record: RecordNew) {
        recordList.add(itemCount, record)
        notifyItemRangeInserted(itemCount, 1)
        mRecycleView.layoutManager!!.scrollToPosition(itemCount - 1)
        println(recordList)
    }

    fun removeMessage(id: Long) {
        println("需要撤回的消息的ID为 $id")
        if (recordList.size <= 0) return
        var i = 0
        while (recordList.size > i) {
            if (recordList[i].snowFlower == id) {
                recordList.remove(recordList[i])
                notifyItemRemoved(i)
                break
            }
            i++
        }
    }

    fun setID(id: Long, timeStamp: Long) {
        if (recordList.size <= 0) return
        var i = 0
        while (recordList.size > i) {
            if (recordList[i].date == timeStamp) {
                recordList[i].snowFlower = id
                break
            }
            i++
        }
    }
}